const express = require('express')

//导入加密模块
const crypto = require("crypto")

// token
const jwt = require('jsonwebtoken')

const main = express.Router()

const User = require('../db/model/User')
const {
    ok
} = require('assert')


// 注册
// main.post('/register',require('./router/register'))
// 登录
// main.post('/login',require('./router/login'))

// 注册
main.post('/register', async (req, res) => {
    console.log("14:", req.body)
    const {
        uname,
        phone,
        password,
    } = req.body
    //密码加密
    let md5 = crypto.createHash("md5");
    let newPas = md5.update(password).digest("hex");
    // console.log("16:",typeof(phone));
    // 第一个uphone是数据库的关键词，第二个phone是用户发送的数据
    let model = await User.findOne({
        where: {
            uphone: phone
        }
    })
    console.log("18", model);
    if (model) {
        var fail = {
            message: "注册失败,手机号已存在",
            code: 5000
        }
        res.send(fail)
    } else {
        var succ = {
            message: "注册成功",
            code: 200
        }
        const adduser = await User.create({
            uname: uname,
            uphone: phone,
            upassword: newPas
        })
        // console.log(adduser);
        res.send(succ)
    }
})


// 登录
main.post('/login', async (req, res) => {
    console.log("47:", req.body);
    const {
        phone,
        password
    } = req.body
    //密码加密
    let md5 = crypto.createHash("md5");
    let newPas = md5.update(password).digest("hex");
    let model = await User.findOne({
        where: {
            uphone: phone,
            upassword: newPas
        }
    })
    console.log("53", model);
    if (model != null) {
        // 生成token  第一个数uphone为数据库的数据，第二个数phone为req.body的数据  第三个数为秘钥，解析时应该相同
        const token = jwt.sign({
            uphone: phone
        }, 'nodestudy')
        var succ = {
            message: "登录成功",
            code: 200,
            token: token
        }
        res.send(succ)
    } else {
        var fail = {
            message: "登录失败,账号或密码错误",
            code: 5000
        }
        res.send(fail)
    }



})

// token验证测试
main.post('/auth', async (req, res) => {
    console.log("user", req.headers);
    // const token = String(req.headers.authorization).split("").pop()
    const token = String(req.headers.authorization)
    if (!token) {
        var fail = {
            message: "无token",
            code: 6000
        }
        res.send(fail)
    } else {
        // 解密 nodestudy为秘钥import应该和生成的秘钥相同
        const {
            uphone
        } = jwt.verify(token, 'nodestudy')
        // res.send('ok')
        const model = await User.findOne({
            where: {
                uphone
            }
        })
        console.log(model);
        if (model != null) {
            res.send("ok")
        } else {
            var fail = {
                message: "用户不存在，请注册",
                code: 5000
            }
            res.send(fail)
        }
    }


})


module.exports = main